package com.situ.personal.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.personal.entity.PageInfo;
import com.situ.personal.entity.Role;
import com.situ.personal.entity.User;
import com.situ.personal.service.RoleService;
import com.situ.personal.service.UserRoleService;
import com.situ.personal.service.UserService;

@WebServlet("/userRole")
public class UserRoleServlet extends HttpServlet{
	UserRoleService userRoleService = new UserRoleService();
	UserService userService = new UserService();
	RoleService roleService = new RoleService();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		// 处理Post请求的乱码
		// req.setCharacterEncoding("UTF-8");
		
		// 判断当前是否 是 登录状态
		HttpSession httpSession = req.getSession(); // 获取那块Session
		User user = (User) httpSession.getAttribute("user");
		if (user==null) {
			System.out.println("UserServlet当前是否登陆, user:"+user);
			resp.sendRedirect(req.getContextPath()+"/user_login.jsp");
			return;
		}
		
		
		// 接收 方法
		String method  = req.getParameter("method");
		
		//如果 忘记传入 挥着 第一次进入   默认值 findByPage
		if ( method == null || "".equals(method) ) {
			method = "findByPage";
		}
		System.out.println("UserServlet现在执行的方法"+ method);
		switch (method) {
		case "findByPage":
			findByPage(req,resp);
			break;
		case "getUserRoleInsertPage":  //添加之前   先到这里面   获取xxx封装起来  拿到添加的jsp页面可以  遍历一些字段
			getUserRoleInsertPage(req, resp);
			break;
		case "insert":
			insert(req,resp);
			break;
		case "deleteById":
			deleteById(req,resp);
			break;
//		case "selectById":
//			selectById(req,resp);
//			break;
//		case "updateById":
//			updateById(req,resp);
//			break;

		default:
			break;
		}
		
	}// service 

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String userId = req.getParameter("userId");
		String roleId = req.getParameter("roleId");
		
		boolean isSuccess = userRoleService.deleteById(Integer.parseInt(userId),Integer.parseInt(roleId));
		if (isSuccess) {
			System.out.println("user_role删除成功");
		}else {
			System.out.println("user_role删除失败");
		}
		// 删除完毕   重定向  重新访问
		resp.sendRedirect(req.getContextPath()+"/userRole");
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 1个userId 对应 多个roleId      所以转发  转1个userId 和 对应的多个RoleId(数组的形式) 
		String userId = req.getParameter("userId");  // 1
		String[] roleIds = req.getParameterValues("roleId"); //  多  接收数组 多个RoleId值
		System.out.println("添加的userId: "+ userId);
		System.out.println("添加的roleId: "+ Arrays.toString(roleIds));
		
		ArrayList<Integer> roleIdsList = new ArrayList<Integer>();
		for (String roleId: roleIds) {
			roleIdsList.add(Integer.parseInt(roleId));
		}
		
		boolean isSueesss =  userRoleService.insert(Integer.parseInt(userId),roleIdsList);
		if (isSueesss) {
			System.out.println("添加成功");
		}else {
			System.out.println("添加失败");
		}
		// 插入成功
		
		resp.sendRedirect(req.getContextPath()+"/userRole");
	} //inSert

	private void getUserRoleInsertPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		// 拿到 所有 user
		// 拿到 所有 role
		// 两个 放进作用域
		// 跳转到  插入页面
		List<User> userList = userService.findAll();
		List<Role> roleList = roleService.findAll();
		
		req.setAttribute("userList", userList);
		req.setAttribute("roleList", roleList);
		req.getRequestDispatcher("/user_role_insert.jsp").forward(req, resp); //  *#*# 跳转到插入页面
	}

	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		PageInfo pageInfo = userRoleService.findByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		System.out.println(pageInfo);
		
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/user_role_page_list.jsp").forward(req, resp);	
//		ArrayList<User> list = (ArrayList<User>) pageInfo.getList();
//		req.setAttribute("list", list);
//		req.getRequestDispatcher("/JSTL_.jsp").forward(req, resp);
	}
	

}
